//
//  CrosswiseRollView.m
//  MyScrollView
//
//  Created by iOS on 2017/10/26.
//  Copyright © 2017年 iOS. All rights reserved.
//

#import "CrosswiseRollView.h"
#import "CrosswiseCFL.h"
#import "CrosswiseCollectionViewCell.h"
#import "CrosswiseFrameModel.h"
#import "UIImageView+WebCache.h"

#define MAX_SECTION 1000
@interface CrosswiseRollView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) CrosswiseFrameModel *frameModel;
@end
@implementation CrosswiseRollView
static NSString * const reuseIdentifier = @"CrosswiseCollectionViewCell";

- (instancetype)initImageUrls:(NSArray *) images {
    self = [super init];
    if (self) {
        _images = images;

        //拿到布局的模型
        self.frameModel = [CrosswiseFrameModel getCrosswiseFrameModel];
        //大小
        self.frame = CGRectMake(0, 0,SCREEN_WIDTH , self.frameModel.collectionViewHigth);
        //UICollectionViewLayout定制
        CrosswiseCFL *crosswissCFL = [[CrosswiseCFL alloc] init];
        //初始化
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , self.frameModel.collectionViewHigth) collectionViewLayout:crosswissCFL];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = FALSE;
        _collectionView.showsHorizontalScrollIndicator = FALSE;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[CrosswiseCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
        [self addSubview:_collectionView];
        
        NSInteger row = MAX_SECTION / 2;
        row = row - row % _images.count;
        NSLog(@"row = %ld",row);
        //滚动到中间位置
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:row inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
        [_collectionView layoutIfNeeded];

    }
    return self;
}

/*
 *  MARK:collectionView 代理
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return MAX_SECTION;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CrosswiseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    NSString *url = _images[indexPath.row%_images.count];
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:url]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.frameModel.cellWidth,self.frameModel.cellHeight);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld",indexPath.row%_images.count);
}




@end
